import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../../../pojo/ImPojo.dart';
import '../../../../util/ImConstant.dart';
import '../../../bloc/TransferMessageCubit.dart';
import '../../../sub/TransferMessagePage.dart';
import '../SessionIconView.dart';

class SessionItemSelectView extends StatelessWidget {

  final ImSession imSession;
  final SelectType selectType;

  const SessionItemSelectView({required this.imSession, required this.selectType, super.key});

  @override
  Widget build(BuildContext context) {
    var transferMessageCubit = context.read<TransferMessageCubit>();
    return ListTile(
      leading: _buildLeading(context, transferMessageCubit),
      title: Text('${imSession.name}',
          style: const TextStyle(fontSize: ImConstant.IM_DEFAULT_SIZE, fontWeight: FontWeight.w500)),
      onTap: (){
        if (selectType == SelectType.multiple) {
          if (transferMessageCubit.state.selectedSessions.contains(imSession)) {
            transferMessageCubit.add(TransferMessageRemoveSessionEvent(imSession));
          } else {
            transferMessageCubit.add(TransferMessageAddSessionEvent(imSession));
          }
        } else {
          transferMessageCubit.add(TransferMessageSelectSessionEvent(imSession));
        }
      },
    );
  }

  Widget _buildLeading(BuildContext context, TransferMessageCubit transferMessageCubit) {
    if (selectType == SelectType.multiple) {
      return SizedBox(
        width: 90,
        child: Row(
          children: [
            Radio(
              value: imSession.id!,
              groupValue: transferMessageCubit.state.selectedSessions.contains(imSession) ? imSession.id! : 0,
              onChanged: (int? value) {
                if (transferMessageCubit.state.selectedSessions.contains(imSession)) {
                  transferMessageCubit.add(TransferMessageRemoveSessionEvent(imSession));
                } else {
                  transferMessageCubit.add(TransferMessageAddSessionEvent(imSession));
                }
              },
            ),
            SessionIconView(session: imSession, iconSize: 40,)
          ],
        ),
      );
    } else {
      return SessionIconView(session: imSession, iconSize: 40,);
    }
  }
  
}